package cn.edu.leetcode.单链表;


import java.util.Objects;

public class 合并两个排序的链表 {
    public static void main(String[] args) {

    }



    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode root = new ListNode();
        ListNode p = root;

        while (Objects.nonNull(l1) && Objects.nonNull(l2)) {
            if (l1.val < l2.val){
                p.next = l1;
                l1 = l1.next;
            } else {
                p.next = l2;
                l2 = l2.next;
            }
            p = p.next;
        }

        if (Objects.nonNull(l1)) p.next = l1;
        if (Objects.nonNull(l2)) p.next = l2;

        return root.next;
    }
}
